package com.fitbet.wechat.handler.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.fitbet.entity.bet.Bet;
import com.fitbet.entity.bet.BetComponent;
import com.fitbet.wechat.handler.api.BetApiHandler;
import com.fitbet.wechat.rao.api.BetRao;
import com.fitbet.wechat.rao.api.UserBetRao;

@Component("betApiHandler")
public class BetApiHandlerImpl implements BetApiHandler {
	
    @Value("#{ctx['api.host']}")
    private String apiHost;
    
    @Rao
    private BetRao betRao;

    @Rao
    private UserBetRao userBetRao;
    
    /*
     * (non-Javadoc)
     * @see com.fitbet.wechat.handler.api.BetApiHandler#loadCurrentBet()
     */
	public Bet loadCurrentBet() {
		
		HttpResult httpResult = betRao.loadCurrentBet(apiHost + "/bets/current");
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        Bet bet = JsonUtils.parse(JsonUtils.toJson(httpResult.get("bet")), Bet.class);
        
        return bet;
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.wechat.handler.api.BetApiHandler#listHotUserBet()
	 */
	public List<BetComponent> listHotUserBet() {
		
//		HttpResult httpResult = betRao.loadCurrentBet(apiHost + "/user-bets/hot");
		
		HttpResult httpResult = userBetRao.listHotUserBet(apiHost+"/user-bets/hot");
        
        if(!httpResult.isValid()) {
            throw BusinessException.newInstance(httpResult.getCode(), httpResult.getMessage());
        }
        
        List<BetComponent> betComponents = new ArrayList<BetComponent>();        
        
//        LIST<USERBET> USERBETS = JSONUTILS.PARSEASLIST(JSONUTILS.TOJSON(HTTPRESULT.GET("USERBET")), USERBET.CLASS);
        
//        FOR (USERBET USERBET : USERBETS) {
//        	
//        	
//        }
        
        return betComponents;
	}

}
